<?php

namespace App\Http\Controllers\Mobile\Index;
use App\Models\Api\Interview;
use App\Models\Common\CollTown;
use App\Models\Company\CompanyJob;
use App\Models\Company\CompanyUser;
use App\Models\Personal\BrowseCompany;
use App\Models\Personal\BrowseJob;
use App\Models\XS\JobXS;
use Illuminate\Http\Request;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\CollPosition;
use Style,DB,Site,Config;
use App\Http\Controllers\Controller;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyPhoto;
use App\Models\Company\CompanyContact;
use App\Models\Company\CompanyDepartment;
use App\Models\Personal\ResumeSend;
use App\Models\Personal\Resume;
use App\Models\Personal\Favorites;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;



class CompanyJobController extends CommonController{
    /**
     * 二维数组查询值是否存在
     * @param $value
     * @param $array
     * @param $key
     * @return bool
     */
    protected function inToArray($value,$array,$key){
        foreach($array as $k=>$v){
            if($v[$key]==$value){
                return true;
            }
        }
        return false;
    }

    /**
     * 找工作列表
     * @param Request $request
     * @return mixed
     */
    public function job(Request $request){
        $welfares =[['value'=>'','text'=>'不限'],['value'=>1,'text'=>'五险'], ['value'=>2,'text'=>'住房公积金'],
            ['value'=>3,'text'=>'包吃'], ['value'=>4,'text'=>'包住'], ['value'=>5,'text'=>'周末双休'],
            ['value'=>6,'text'=>'带薪年假'],['value'=>7,'text'=>'交通补助'],['value'=>8,'text'=>'周末双休']];

        $salarys=[['value'=>'','salary'=>['',''],'text'=>'不限'],
            ['value'=>1,'salary'=>['0','2000'],'text'=>'2000以下'],
            ['value'=>2,'salary'=>['2000','4000'],'text'=>'2000-4000'],
            ['value'=>3,'salary'=>['4000','6000'],'text'=>'4000-6000'],
            ['value'=>4,'salary'=>['6000','8000'],'text'=>'6000-8000'],
            ['value'=>5,'salary'=>['8000','10000'],'text'=>'8000-1万'],
            ['value'=>6,'salary'=>['10000','15000'],'text'=>'1万-1.5万'],
            ['value'=>7,'salary'=>['15000','20000'],'text'=>'1.5万-2万'],
            ['value'=>8,'salary'=>['20000','30000'],'text'=>'2万-3万'],
            ['value'=>9,'salary'=>['30000','40000'],'text'=>'3万-4万'],
            ['value'=>10,'salary'=>['40000','50000'],'text'=>'4万-5万'],
            ['value'=>11,'salary'=>['50000','1000000'],'text'=>'5万以上']];

        $input = $request->only(
            'jobsort1', 'jobsort2', 'jobsort3', 'jobsort4', 'jobsort5',
            'jobarea1','jobarea2','jobarea3','jobarea4','jobarea5',
            'industry1','industry2','industry3','industry4','industry5',
            'sex','age','trade','work_year','education','keyword','keyword_type','release_date','welfare',
            'salary',
            'graduate','anxious','brandcompany','nearby','position_denied'
        );
        //地区搜索
        $webareacode = Site::get('webareacode');//读取地区编码
        if(substr($webareacode, -2, 2) != '00'){
            $areas = CollTown::getTown($webareacode);//返回乡镇地区
        }else{
            $areas = CollArea::getCity($webareacode);//返回县级市地区
        }
        array_unshift($areas, ['value' => '', 'text' => "不限"]);

        if(!empty($request->location1)){
            $request->jobarea1=$request->location1;//解决个人后台input冲突临时方案
            $input['jobarea1']=$request->location1;
        }
        //附近工作
        $nearby = $request->nearby;
        $position_denied = !empty($request->position_denied) ? $request->position_denied : '';
        $jobTitle  = empty($nearby) ? '找工作' : '附近工作';
        if(!empty($nearby)){
            //经度纬度
            $_lng=!empty($request->_lng)? $request->_lng : Site::get('longitude');
            $_lat=!empty($request->_lat)? $request->_lat : Site::get('latitude');

            $juli=DB::raw("(2 * 6378.137* ASIN(SQRT(POW(SIN(PI()*(".$_lat."-".env('DB_PREFIX')."t2.latitude)/360),2)+COS(PI()*".
                $_lat."/180)* COS(".env('DB_PREFIX')."t2.latitude * PI()/180)*POW(SIN(PI()*(".$_lng."-".env('DB_PREFIX')."t2.longitude)/360),2)))) as juli");
            $order1=DB::raw('case when juli is null then 1 else 0 end ,juli');
            $orderBy1 = 'asc';
            $order2 = "id";
            $orderBy2 = 'desc';
            //为xunsearch讯搜加上去的
            $request->query->set('longitude',$_lng);
            $request->query->set('latitude',$_lat);
            $request->query->set('map_show',$nearby);
        }else{
            $juli="t2.contacts";
            $order1 = "t1.updatetime";
            $order2 = "t1.id";
            $orderBy1 = 'desc';
            $orderBy2 = 'desc';
        }

        //去空
        $pageInput = arrayFilter($input);
        $this->urlArray = $pageInput;
        /**_______________选择搜索引擎_________________***/
        //处理数据查询
        if (Site::get('xsOpen') && (!empty($request->keyword) || !empty($request->jobsort1))) {
            $company_job=$this->xunSearch($request, $welfares, $salarys);
            if(!$company_job->total()) $company_job=$this->sqlSearch($request, $welfares, $salarys, $juli, $order1, $order2, $orderBy1, $orderBy2, $nearby);
        } else {
            $company_job=$this->sqlSearch($request, $welfares, $salarys, $juli, $order1, $order2, $orderBy1, $orderBy2, $nearby);
        }
        $page = $company_job->appends($pageInput)->links();//分页链接
        /**_______________选择搜索引擎结束*******************/

        //去空
        $pageInput=arrayFilter($input);
        $this->urlArray=$pageInput;
        $page= $company_job->appends($pageInput)->links();//分页链接


        //地区搜索
        $webareacode =Site::get('webareacode');//读取地区编码
        $areas = CollArea::getCity($webareacode);//返回地区下编码
        array_unshift($areas,['value'=>'','text'=>"不限"]);

        //生成地区链接数组
        $checkarray=array($request->jobarea1,$request->jobarea2,$request->jobarea3,$request->jobarea4,$request->jobarea5);

        foreach($areas as $k=>$v){
            $area[$k]['url']=urlPagePack($pageInput,['jobarea1'=>$v['value'],'jobarea2'=>null,
                'jobarea3'=>null,'jobarea4'=>null,'jobarea5'=>null]);
            $area[$k]['text']=$v['text'];
            if(in_array($v['value'],$checkarray) and $v['value']!=''){
                $area[$k]['check'] = 1;
            }else{
                if($v['value']=='' and $request->jobarea1==''){
                    $area[$k]['check'] = 1;
                }else{
                    $area[$k]['check'] = 0;
                }
            }
        }
        foreach($checkarray as $k=>$v){
            if($v!=null and !$this->inToArray($v,$areas,'value')){
                $area[]=array(
                    'url'=>urlPagePack($pageInput,['jobarea1'=>$v,'jobarea2'=>null,
                        'jobarea3'=>null,'jobarea4'=>null,'jobarea5'=>null]),
                    'text'=>CollArea::getAreaName($v),
                    'check'=>1,
                );
            }
        }

        $optionsurl['jobarea']= urlPagePack($pageInput,['jobarea1'=>null,'jobarea2'=>null,
            'jobarea3'=>null,'jobarea4'=>null,'jobarea5'=>null]);

        //福利链接数组成生
        $welfare=array();
        foreach($welfares as $k=>$v){
            $welfare[$k]['url']=urlPagePack($pageInput,['welfare'=>$v['value']]);
            $welfare[$k]['text']=$v['text'];
            $welfare[$k]['value']=$v['value'];
        }
        //月薪
        foreach($salarys as $k=>$v){
            $salary[$k]['url']=urlPagePack($pageInput,['salary'=>$v['value']]);
            $salary[$k]['text']=$v['text'];
            $salary[$k]['value']=$v['value'];
        }
        //工地岗位转中文
        $jobsort1 = ($request->jobsort1) ?  CollPosition::getPosName($request->jobsort1) : '';
        $jobsort2 = ($request->jobsort2) ?  CollPosition::getPosName($request->jobsort2) : '';
        $jobsort3 = ($request->jobsort3) ?  CollPosition::getPosName($request->jobsort3) : '';
        $jobsort4 = ($request->jobsort4) ?  CollPosition::getPosName($request->jobsort4) : '';
        $jobsort5 = ($request->jobsort5) ?  CollPosition::getPosName($request->jobsort5) : '';
        $cninput['jobsort'] = trim($jobsort1 . '+' . $jobsort2 . '+'
            . $jobsort3 . '+'  . $jobsort4 . '+'  . $jobsort5,'+');
        $optionsurl['jobsort'] = urlPagePack($pageInput,['jobsort1'=>null,'jobsort2'=>null,
            'jobsort3'=>null,'jobsort4'=>null,'jobsort5'=>null]);
        //行业
        $industry1 = ($request->industry1) ?  CollOption::getOptionName($request->industry1,'industry') : '';
        $industry2 = ($request->industry2) ?  CollOption::getOptionName($request->industry2,'industry') : '';
        $industry3 = ($request->industry3) ?  CollOption::getOptionName($request->industry3,'industry') : '';
        $industry4 = ($request->industry4) ?  CollOption::getOptionName($request->industry4,'industry') : '';
        $industry5 = ($request->industry5) ?  CollOption::getOptionName($request->industry5,'industry') : '';
        $cninput['industry'] = trim($industry1 . '+' . $industry2 . '+' . $industry3 .
            '+'  . $industry4 . '+'  . $industry5,'+');
        $optionsurl['industry'] = urlPagePack($pageInput,['industry1'=>null,'industry2'=>null,
            'industry3'=>null,'industry4'=>null,'industry5'=>null]);
        //工作经验
        $optionsurl['work_year'] = urlPagePack($pageInput,['work_year'=>null]);
        //学历
        $optionsurl['education'] = urlPagePack($pageInput,['education'=>null]);
        //急招
        $optionsurl['anxious'] = urlPagePack($pageInput,['anxious'=>null]);
        //名企
        $optionsurl['brandcompany'] = urlPagePack($pageInput,['brandcompany'=>null]);
        //搜索关键字
        $optionsurl['keyword'] = urlPagePack($pageInput,['keyword'=>null]);
        //搜索月薪
        $optionsurl['salary'] = urlPagePack($pageInput,['salary'=>null]);

        //压入中文到数组
        $cnoption['jobarea1']=CollArea::getAreaName($request->jobarea1);
        $cnoption['jobarea2']=CollArea::getAreaName($request->jobarea2);
        $cnoption['jobarea3']=CollArea::getAreaName($request->jobarea3);
        $cnoption['jobarea4']=CollArea::getAreaName($request->jobarea4);
        $cnoption['jobarea5']=CollArea::getAreaName($request->jobarea5);
        $cnoption['jobarea'] = trim( $cnoption['jobarea1'] . '+' .  $cnoption['jobarea2'] . '+' .  $cnoption['jobarea3'] .
            '+'  .  $cnoption['jobarea4'] . '+'  .  $cnoption['jobarea5'],'+');

        if(!empty($welfares[$request->welfare])) $cnoption['welfare']=$welfares[$request->welfare]['text'];//福利
        if(!empty($salary[$request->salary])) $cnoption['salary']=$salary[$request->salary]['text'];//月薪



        $cnoption['jobsort1']=$jobsort1;
        $cnoption['jobsort2']=$jobsort2;
        $cnoption['jobsort3']=$jobsort3;
        $cnoption['jobsort4']=$jobsort4;
        $cnoption['jobsort5']=$jobsort5;

        $cnoption['jobsort']=$cninput['jobsort'];


        $cnoption['industry1']=$industry1;
        $cnoption['industry2']=$industry2;
        $cnoption['industry3']=$industry3;
        $cnoption['industry4']=$industry4;
        $cnoption['industry5']=$industry5;

        $cnoption['industry']=$cninput['industry'];

        $cnoption['work_year']=CollOption::getOptionName($request->work_year,'work_year');
        $cnoption['education']=CollOption::getOptionName($request->education,'education');

        $cnoption['anxious']='';
        $cnoption['brandcompany']='';
        $cnoption['keyword_type']='';
        $cnoption['keyword']=msubstr($input['keyword'],0,10);
        $cnoption['page']='';

        //生成去除条件链接
        $del_list=array();
        foreach($pageInput as $key=>$value){
            $ikey=substr($key,0,-1);
            if($ikey!='jobarea' and $ikey!='jobsort' and $ikey!='industry'){
                if(!empty($cnoption[$key])){
                    $del_list[]=array('text'=>$cnoption[$key],'url'=>urlPagePack($pageInput,[$key=>null]));
                }
            }else{
                if(substr($key,-1,1)==1){
                    $key1=$ikey.'1';$key2=$ikey.'2';$key3=$ikey.'3';$key4=$ikey.'4';$key5=$ikey.'5';
                    $del_list[]=array('text'=>msubstr($cnoption[$ikey],0,10),'url'=>urlPagePack($pageInput,
                        [$key1=>null,$key2=>null,$key3=>null,$key4=>null,$key5=>null]));
                }
            }
        }

        foreach($company_job as $k => $v) {
            //急聘
            if($v->urgent>date('Y-m-d H:i:s',strtotime('-5 day'))){
                $v->urgent=1;
            }else{
                $v->urgent=0;
            }
            //工资
            if($v->salary_above==0 and $v->salary_below==0){
                $v->salary_above ='面议';
            }else{
                $v->salary_above = CollOption::getOptionName($v->salary_above,'expectedsalary').'-'.CollOption::getOptionName($v->salary_below,'expectedsalary');
            }
            //地区
            $v->location1 = CollArea::getAreaName($v->location1);
            $v->location2 = CollArea::getAreaName($v->location2);
            $v->location3 = CollArea::getAreaName($v->location3);
            $v->location4 = CollArea::getAreaName($v->location4);
            $v->location5 = CollArea::getAreaName($v->location5);
            $v->location =  $v->location1.' '.$v->location2.' '. $v->location3.' '.$v->location4.' '.$v->location5;
            if(trim($v->location)==''){
                $v->location = "地区不限";
            }
            $v->work_year = CollOption::getOptionName($v->work_year,'work_year');
            $v->education = CollOption::getOptionName($v->education,'education');

            //附近工作
            if(!empty($nearby)){
                $juli=ceil(($v->juli)*10)*100;
                if($juli >= '1000'){
                    $v->juli = ($juli/1000).'公里';
                }else if($juli < '1000' && $juli >= '0'){
                    $v->juli = $juli.'米';
                }else{
                    $v->juli = '坐标未获取';
                }
            }
        }
        $request->flash();

        //设置标题
        $area_name = !empty($request->jobarea1) ? $cnoption['jobarea1'] : Site::get('area_name');
        $keyword = !empty($request->jobsort1) ? $jobsort1 : $input['keyword'];
        $job_search = (strlen($keyword) > 0) ? $area_name.$keyword .'招聘|': '';

        return Style::view('mobile.index.job',compact('company_job','page','area','welfare','salary','_lng','_lat',
            'optionsurl','del_list','input','cninput','del_list','nearby','jobTitle','position_denied','job_search'));
    }

    /** 迅搜搜索
     * @param $request
     * @return \Illuminate\Pagination\LengthAwarePaginator
     */
    public function xunSearch($request, $welfares, $salarys)
    {
        //查询条件
        $q = $request->all();

        //福利
        if(!empty($request->welfare)) $q['welfare'] = $welfares[$request->welfare]['text'];

        //薪资范围
        if(!empty($request->salary)){
            $q['salary_above'] = $salarys[$request->salary]['salary'][0];
            $q['salary_below'] = $salarys[$request->salary]['salary'][1];
        }

        //工作地点、所属岗位、行业分类
        $jobarea=[];//工作地点
        $jobsort=[];//所属岗位
        $industry=[];//行业分类
        for ($i=1;$i<=5;$i++) {
            $area=$request->input('jobarea' . $i);
            $sort=$request->input('jobsort' . $i);
            $dustry=$request->input('industry' . $i);
            if (!empty($area)) {
                $jobarea[]=$area;
            }
            if (!empty($sort)) {
                $jobsort[]=$sort;
            }
            if (!empty($dustry)) {
                $industry[]=$dustry;
            }
        }

        //关键字
        if(!empty($request->keyword)){
            if($request->keyword_type == 2){
                $q['job_title'] = $request->keyword;
            }elseif($request->keyword_type == 3){
                $q['company_name'] = $request->keyword;
            }else{
                $q['keyword'] = $request->keyword;
            }
        }

        $q['jobarea']=$jobarea;
        $q['jobsort']=$jobsort;
        $q['industry']=$industry;

        $jobxs = new JobXS();
        //查询页码
        $page = $request->input('page', 1);
        if($page > 200) $page = 200;
        //搜索(参数:搜索条件,第几页,一页个数,获取总数,是否按照时间排序)
        $arr = $jobxs->searchJob($q, $page, 20, true);

        //分页并返回
        return $this->pageInfo($arr['data'], $arr['total'], $request);
    }

    /** 数据库搜索
     * @param $request
     * @return mixed
     */
    public function sqlSearch($request, $welfares, $salarys, $juli, $order1, $order2, $orderBy1, $orderBy2, $nearby)
    {
        $company_job = DB::table('company_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            //获取字段
            ->select(
                't1.id','t1.com_id','t1.job_title','t1.industry',
                't1.job_category1','t1.job_category2','t1.job_category3','t1.job_category4','t1.job_category5',
                't1.location1','t1.location2','t1.location3','t1.location4','t1.location5',
                't1.recruiting_num','t1.salary_above','t1.salary_below','t1.description','t1.welfare','t1.part_status',
                't1.education','t1.work_year','t1.age_above','t1.age_below','t1.gender','t1.updatetime',
                't2.company_name','t2.region','t2.industry','t2.address','t2.employee_num','t1.urgent',$juli

            )
            ->where('job_status',1)
            //岗位
            ->where(function ($query) use ($request) {
                for($i=1;$i<=5;$i++){
                    if(!empty($request->{'jobsort'.$i}))
                        $query->orWhere('t1.job_category1', '=', $request->{'jobsort'.$i})
                            ->orWhere('t1.job_category2', '=', $request->{'jobsort'.$i})
                            ->orWhere('t1.job_category3', '=', $request->{'jobsort'.$i})
                            ->orWhere('t1.job_category4', '=', $request->{'jobsort'.$i})
                            ->orWhere('t1.job_category5', '=', $request->{'jobsort'.$i});
                    else break;
                }
            })
            //工作地点
            ->where(function ($query) use ($request) {
                $city = [];
                for($i = 1; $i <= 5; $i++){
                    if(!empty($request->input('jobarea' . $i))){
                        $query->orWhere('t1.location1', '=', $request->{'jobarea' . $i})
                            ->orWhere('t1.location2', '=', $request->{'jobarea' . $i})
                            ->orWhere('t1.location3', '=', $request->{'jobarea' . $i})
                            ->orWhere('t1.location4', '=', $request->{'jobarea' . $i})
                            ->orWhere('t1.location5', '=', $request->{'jobarea' . $i});
                        $city[] = substr($request->input('jobarea' . $i), 0, 4) . '00';
                    }else{
                        break;
                    }
                }
                if(count($city) > 0){
                    $city = array_unique($city);
                    foreach($city as $v){
                        $query->orWhere('t1.location1', '=', $v)
                            ->orWhere('t1.location2', '=', $v)
                            ->orWhere('t1.location3', '=', $v)
                            ->orWhere('t1.location4', '=', $v)
                            ->orWhere('t1.location5', '=', $v);
                    }
                }
            })
            //所属行业
            ->where(function ($query) use ($request) {
                for($i=1;$i<=5;$i++){
                    if(!empty($request->input('industry'.$i))){
                        $query->orWhere('t1.industry', '=', $request->input('industry'.$i));
                    }
                    else break;
                }
            })
            //福利
            ->where(function ($query) use ($request,$welfares) {
                if(!empty($request->welfare)) $query->where('t1.welfare', 'like', "%".$welfares[$request->welfare]['text']."%");
            })
            //月薪要求
            ->where(function ($query) use ($request,$salarys) {
                if(!empty($request->salary)){
                    $query->where('t1.salary_above', '>=', $salarys[$request->salary]['salary'][0])
                        ->where('t1.salary_above', '<=', $salarys[$request->salary]['salary'][1]);
                }
            })
            //性别要求
            ->where(function ($query) use ($request) {
                if(!empty($request->sex)) $query->where('t1.gender', '=', $request->sex);
            })
            //年龄要求
            ->where(function ($query) use ($request) {
                if(!empty($request->age)){
                    $query->where('t1.age_above', '>=', $request->age);

                }
            })
            //学历要求
            ->where(function ($query) use ($request) {
                if(!empty($request->education)) $query->where('t1.education', '>=', $request->education);
            })
            //年龄要求 当前所选大于或者等于年龄以上的
            ->where(function ($query) use ($request) {
                if(!empty($request->age)) $query->where('t1.age_above', '>=', $request->age);
            })
            //工作经验 当前所选工作经验以上
            ->where(function ($query) use ($request) {
                if(!empty($request->work_year)) $query->where('t1.work_year', '>=', $request->work_year);
            })
            //关键字
            ->where(function ($query) use ($request) {
                if(!empty($request->keyword)){
                    if($request->keyword_type == '2'){
                        $query->where('t1.job_title', 'like', '%'.$request->keyword.'%');
                    }elseif($request->keyword_type == '3'){
                        $query->where('t2.company_name', 'like', '%'.$request->keyword.'%');
                    }else{
                        //全文搜索时？？？？
                        $query->where('t1.job_title', 'like', '%'.$request->keyword.'%')
                            ->orWhere('t2.company_name', 'like', '%'.$request->keyword.'%');
                    }
                }
            })
            //发布日期
            ->where(function ($query) use ($request) {
                if(!empty($request->release_date)){
                    //获取选中的多少时间内
                    $ctime = date("Y-m-d H:i:s",strtotime("-".$request->release_date. "day"));
                    $query->where('t1.updatetime', '>=', $ctime);
                }
            })
            //急招
            ->where(function ($query) use ($request) {
                if(!empty($request->anxious)){
                    //获取选中的多少时间内
                    //$query->where('t1.urgent', '>=', date("Y-m-d H:i:s",time()));
                    $query->where('t1.urgent', '>=', date("Y-m-d H:i:s",strtotime(' -5 day',time())));
                }
            })
            //附近工作只找15天内的
            ->where(function($query) use ($request , $nearby){
                if(!empty($nearby)){
                    //获取选中的多少时间内
                    //$query->where('t1.urgent', '>=', date("Y-m-d H:i:s",time()));
                    $query->where('t1.updatetime', '>=', date("Y-m-d H:i:s", strtotime(' -15 day', time())));
                }

            });
        $count=$company_job->count();

        $company_job = $company_job->orderBy($order1, $orderBy1)
            ->orderBy($order2, $orderBy2)
            ->skip($this->getSkip($request->page))->take(20)->get();


        return  $this->pageInfo($company_job,$count,$request);
    }
    /**
     * 分页信息
     */

    protected function pageInfo($data,$total,$request){
        $perPage = 20;//每页显示20条
        $current_page = $request->input('page',1);
        $total=$total>4000?4000:$total;
        $company_job = new LengthAwarePaginator($data, $total, $perPage, $current_page,[
            'path' => Paginator::resolveCurrentPath(), //注释2
            'pageName' => 'page',
        ]);
        return $company_job;
    }

    /**
     * 取得分页启始数
     */
    protected function getSkip($page){
        if(empty($page)) $page=1;
        if($page>200) $page=200;
        return ($page-1)*20;
    }
    /**
     *
     * 显示职位信息
     * @param $jobid
     * @return mixed
     */
    public function jobInfo($jobid){
        $jobid = idDecryption($jobid);
        $jobinfo = CompanyJob::with(['company_basic'])->where('id','=',$jobid)->first();//当前工作页面详情
        $companyUser = CompanyUser::select('last_login_time', 'viptime')->where('uid', $jobinfo->uid)->first();
        $jobinfo->increment('pageviews');//流量自增
        if($jobinfo->salary_above==0 and $jobinfo->salary_below==0){
            $jobinfo->salary_above ='面议';
        }else{
            $jobinfo->salary_above = '【'.CollOption::getOptionName($jobinfo->salary_above,'expectedsalary').'-'.CollOption::getOptionName($jobinfo->salary_below,'expectedsalary').'元/月】';
        }
        //返回招聘工作地点
        !empty($jobinfo->location1) ?  $jobinfo->location1 = CollArea::getAreaName($jobinfo->location1): $jobinfo->location1='';
        !empty($jobinfo->location2) ?  $jobinfo->location2 = CollArea::getAreaName($jobinfo->location2): $jobinfo->location2='';
        !empty($jobinfo->location3) ?  $jobinfo->location3 = CollArea::getAreaName($jobinfo->location3): $jobinfo->location3='';
        !empty($jobinfo->location4) ?  $jobinfo->location4 = CollArea::getAreaName($jobinfo->location4): $jobinfo->location4='';
        !empty($jobinfo->location5) ?  $jobinfo->location5 = CollArea::getAreaName($jobinfo->location5): $jobinfo->location5='';

        $jobinfo->location =  $jobinfo->location1.' '.$jobinfo->location2.' '. $jobinfo->location3.' '.$jobinfo->location4.' '.$jobinfo->location5;

        if(trim($jobinfo->location)==''){
            $jobinfo->location = "地区不限";
        }

        //返回学历
        !empty($jobinfo->education) ? $jobinfo->education= CollOption::getOptionName($jobinfo->education,'education')  : $jobinfo->education='';

        $jobinfo->work_year = CollOption::getOptionName($jobinfo->work_year,'work_year');
        //年龄要求
        if(!empty($jobinfo->age_above) and !empty($jobinfo->age_below)){
            $jobinfo->age = $jobinfo->age_above.'岁-'.$jobinfo->age_below.'岁';
        }elseif(!empty($jobinfo->age_above) and empty($jobinfo->age_below)){
            $jobinfo->age = $jobinfo->age_above.'岁以上';
        }elseif(empty($jobinfo->age_above) and !empty($jobinfo->age_below)){
            $jobinfo->age = $jobinfo->age_below.'岁以下';
        }else{
            $jobinfo->age = "年龄不限";
        }
        //福利
        if(!empty($jobinfo->welfare)){
            $jobinfo->welfare = explode(',',$jobinfo->welfare);
        }else{
            $jobinfo->welfare =array();
        }
        //logo
        if(!empty($jobinfo->company_basic->logo)){
            $jobinfo->company_basic->logo =  'upload/companylogo/'.$jobinfo->company_basic->logo;
        }else{
            $jobinfo->company_basic->logo = 'default/images/mobile/layout/te.jpg';
        }
        //企业行业
        !empty($jobinfo->industry) ? $jobinfo->industry= CollOption::getOptionName($jobinfo->industry,'industry')  : $jobinfo->industry='';
        //职位发布者信息   0 基本资料 ，1部门，2 独立联系方式，3不显示
        switch($jobinfo->contact_info){
            case "0"://0 基本资料
                if(!empty($jobinfo->company_basic->mobile)){
                    $jobinfo->dial = $jobinfo->company_basic->mobile;
                }else if(!empty($jobinfo->company_basic->phone)){
                    $jobinfo->dial = trim($jobinfo->company_basic->phone,'-');
                }else{
                    $jobinfo->dial = '';
                }
                $jobinfo->mobile = $jobinfo->company_basic->mobile;
                $jobinfo->phone = trim($jobinfo->company_basic->phone,'-');
                $jobinfo->contacts = $jobinfo->company_basic->contacts;
                break;
            case "1"://1部门
                $companydepartment = CompanyDepartment::select('name','mobile','phone','contacts')->where('com_id','=',$jobinfo->com_id)->take(1)->first();
                if (!empty($companydepartment)){
                    if(!empty($companydepartment->mobile)){
                        $jobinfo->dial = $companydepartment->mobile;
                    }else if(!empty($companydepartment->phone)){
                        $jobinfo->dial = trim($companydepartment->phone,'-');
                    }else{
                        $jobinfo->dial = '';
                    }
                    $jobinfo->mobile = $companydepartment->mobile;
                    $jobinfo->phone = trim($companydepartment->phone,'-');
                    $jobinfo->contacts = $companydepartment->contacts;
                }else{
                    $jobinfo->mobile = '';
                    $jobinfo->phone = '';
                    $jobinfo->contacts ='';
                    $jobinfo->dial = '';
                }
                break;
            case "2"://独立联系方式
                $companycontact = CompanyContact::select('mobile','phone','contacts')->where('job_id','=',$jobinfo->id)->first();
                if(!empty($companycontact->mobile)){
                    $jobinfo->dial = $companycontact->mobile;
                }else if(!empty($companycontact->phone)){
                    $jobinfo->dial = trim($companycontact->phone,'-');
                }else{
                    $jobinfo->dial = '';
                }
                $jobinfo->mobile = $companycontact->mobile;
                $jobinfo->phone = trim($companycontact->phone,'-');
                $jobinfo->contacts = $companycontact->contacts;
                break;
            case "3"://3不显示
                $jobinfo->mobile = '';
                $jobinfo->phone = '';
                $jobinfo->contacts = '保密';
                break;
        }
        //申请职位、
        $user = auth()->guard('personal')->user();
        if($user){
           //登录-检测是否已申请该职位

            $applyJob = ResumeSend::select('status')->where('job_id',$jobid)->where('personal_uid',$user->uid)->first();//申请职位信息
            if($applyJob){//申请职位
                $applyString = "已投简历";
            }else{
                $applyString = "投递简历";
            }
            $applyJobButton = "applyjob";


            //添加job浏览记录   job_id  personal_uid  resume_id
            if(auth()->guard('personal')->check()){//登录
                $Resume = Resume::select("id")->where("uid", $user->uid)->orderBy("updatetime", "desc")->first();
                if(!empty($Resume->id)){
                    if(!BrowseJob::where("job_id", $jobid)->where("personal_uid", $user->uid)->where("resume_id", $Resume->id)->first()){
                        $addBrowseJob = [];
                        $addBrowseJob["company_uid"] = $jobinfo->uid;
                        $addBrowseJob["com_id"] = $jobinfo->com_id;
                        $addBrowseJob["personal_uid"] = $user->uid;
                        $addBrowseJob["resume_id"] = $Resume->id;
                        $addBrowseJob["job_id"] = $jobid;
                        BrowseJob::create($addBrowseJob);
                    }
                }
            }
        }else{
            $applyJobButton = "personlogin";
            $applyString = "投递简历";
        }
        //公司信息
        $companyBasic = CompanyBasic::where('id','=',$jobinfo->com_id)->first(['id','longitude','latitude','coordinate_address']);//公司信息
        $jobinfo->strmobile = (!auth()->guard('personal')->check()  && Site::get('viewContact') =='2')  ?
            preg_replace('/(\d{3})(.*)(\d{4})/i','$1****$3',$jobinfo->mobile):'';
        $jobinfo->strphone = (!auth()->guard('personal')->check() && Site::get('viewContact') =='2') ?
            preg_replace('/(\d{3})(.*)(\d{3})/i','$1****$3',$jobinfo->phone):'';

        //查询该面试记录是否被查看
        if(!empty($user)){
            $Interview = Interview::where('personal_uid',$user->uid)->where('job_id',$jobid)->where('status',0)->orderBy('created_at','desc')->get();
            if(count($Interview) > 0){
                Interview::where('personal_uid',$user->uid)->where('job_id',$jobid)->update(['status'=>1]);
            }
        }
        //设置县级市
        $jobinfo->company_basic->region = !empty($jobinfo->company_basic->region) ?  CollArea::getAreaName($jobinfo->company_basic->region) : '';
        $jobinfo->company_basic->address = $jobinfo->company_basic->region.'/'.$jobinfo->company_basic->address;
        if($jobinfo){
            return Style::view('mobile.index.jobInfo',compact('jobinfo','applyJobButton','applyString','jobid','companyBasic','companyUser'));
        }else{
            abort(404);
        }
    }
    //显示企业介绍
    public function comAbout($cid){
        $cid = idDecryption($cid);
        $companyinfo = CompanyBasic::where('id','=',$cid)->first();//公司信息
        //企业规模
        $companyinfo->employee_num = CollOption::getOptionName($companyinfo->employee_num,'employee_num');
        //性质
        $companyinfo->comkind = CollOption::getOptionName($companyinfo->comkind,'comkind');
        //行业
        $companyinfo->industry = CollOption::getOptionName($companyinfo->industry,'industry');
        $temptation = ''; //福利
        if(!empty($companyinfo->temptation)){
            $companyinfo->temptation =explode(',',$companyinfo->temptation);
        }else{
            $companyinfo->temptation =array();
        }
        //公司其它招聘信息
        $jobinfo =CompanyJob::with(['company_basic'])->where('com_id',$companyinfo->id)->where('job_status',1)->orderBy('updatetime','desc')->paginate(10);//公司其它招聘信息

        if(!isset($companyinfo) && count($jobinfo)<=0){
            abort(404);
        }

        foreach($jobinfo as $v){
            $v->job_title = msubstr($v->job_title,0,20);
            //工资
            $v->salary_above = $this->numTostring($v->salary_above);
            $v->salary_below = $this->numTostring($v->salary_below);
            //地区
            !empty($v->location1) ?  $v->location1= CollArea::getAreaName($v->location1): $v->location1='';
            !empty($v->location2) ?  $v->location2= CollArea::getAreaName($v->location2): $v->location2='';
            !empty($v->location3) ?  $v->location3= CollArea::getAreaName($v->location3): $v->location3='';
            !empty($v->location4) ?  $v->location4= CollArea::getAreaName($v->location4): $v->location4='';
            !empty($v->location5) ?  $v->location5= CollArea::getAreaName($v->location5): $v->location5='';
            //学历
            !empty($v->education) ? $v->education= CollOption::getOptionName($v->education,'education')  : $v->education='';
           //工作经验
            !empty($v->work_year) ? $v->work_year= CollOption::getOptionName($v->work_year,'work_year')  : $v->work_year='';
            if(trim($v->location)==''){
                $v->location = "地区不限";
            }
            //工资
            if($v->salary_above==0 and $v->salary_below==0){
                $v->salary_above ='面议';
            }else{
                $v->salary_above =  CollOption::getOptionName($v->salary_above,'expectedsalary').'-'.CollOption::getOptionName($v->salary_below,'expectedsalary').'元';
            }
            //急聘
            if($v->urgent>date('Y-m-d H:i:s',strtotime('-5 day'))){
                $v->urgent=1;
            }else{
                $v->urgent=0;
            }
        }
        $user = auth()->guard('personal')->user();
        if(auth()->guard('personal')->check()){//登录
            //添加job浏览记录   job_id  personal_uid  resume_id
            $Resume = Resume::select("id")->where("uid", $user->uid)->orderBy("updatetime", "desc")->first();
            if(!empty($Resume->id)){
                if(!BrowseCompany::where("com_id", $cid)->where("personal_uid", $user->uid)->where("resume_id", $Resume->id)->first()){
                    $addBrowseCompany = [];
                    $addBrowseCompany["company_uid"] = $companyinfo->uid;
                    $addBrowseCompany["com_id"] = $companyinfo->id;
                    $addBrowseCompany["personal_uid"] = $user->uid;
                    $addBrowseCompany["resume_id"] = $Resume->id;
                    BrowseCompany::create($addBrowseCompany);
                }
            }
        }

        return Style::view('mobile.index.comAbout',compact('companyinfo','jobinfo'));

    }

    /**
     * 万转数字
     * @param $num
     * @return float
     */
    private function floatNum($num){
        $float_num =  floatval($num);
        $explode_float_num = explode('.',$float_num);
        if(!empty($explode_float_num[1])){
            $float_num=$explode_float_num[0]*10000;
        }elseif(!empty($explode_float_num[0]) and strlen($explode_float_num[0])<=2){
            $float_num=$float_num*10000;
        }
        return $float_num;
    }

    /**
     * 数字转万
     * @param $num
     * @return string
     */
    public function numTostring($num){
        $numstring='';
        if(!empty($num)){
            if(strlen($num)>=5){
                $numstring = floatval($num/10000).'万';
            }else{
                return $num;
            }
        }
        return $numstring;
    }

    /**
     * 返回时间差
     * @param $time
     * @return string
     */
    public function timediff($time){
        $timezone = Config::get('app.timezone','PRC');
        date_default_timezone_set($timezone);
        $zero1 = strtotime(date("Y-m-d H:i:s",time()));
        $zero2 = strtotime($time);
        if(($zero1-$zero2)/3600<1){
            $time_c=ceil(($zero1-$zero2)/3600*60).'分钟前';
        }elseif(($zero1-$zero2)/3600>=24){
            $time_c=intval((($zero1-$zero2)/3600)/24).'天前';
        }else{
            $time_c=intval(($zero1-$zero2)/3600).'小时前';
        }
        return $time_c;
    }
}